package cc.site.main.controller;

import cc.site.common.utils.R;
import cc.site.main.bean.dto.PetDto;
import cc.site.main.service.TblKindService;
import cc.site.main.service.TblPetService;
import cc.site.main.service.TblRoomService;
import cc.site.main.service.TblUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @CREATE By yangruomao On 2024/4/18 上午9:26
 */
@RestController
@RequestMapping("/search")
@Api(tags = {"全局搜素宠物、按用户、房间、种类搜索宠物"})
public class SearchController {

    @Autowired
    private TblUserService userService;

    @Autowired
    private TblRoomService roomService;

    @Autowired
    private TblKindService kindService;

    @Autowired
    private TblPetService petService;

    @GetMapping("/pet/{page}/{limit}/{state}")
    @ApiOperation("直接搜索宠物，【1. 返回值中 masterMan 为 null 表示还没有主人。2. 分页，page从1开始，limit为每页数量。\n" +
            "3. state=1:还没被领养的，state=2：已经被领养的，state=3：全部】")
    public R searchPet(@PathVariable("page") Integer page,
                  @PathVariable("limit") Integer limit,
                  @PathVariable("state") Integer state) {
        page = (page - 1) * limit;

        // 查询宠物信息
        List<PetDto> pet = petService.all();

        if (state.equals(1)) {
            return R.ok().data("page", pet.stream().filter(x -> x.getMasterMan() == null).skip(page).limit(limit).collect(Collectors.toList()));
        }

        if (state.equals(2)) {
            return R.ok().data("page", pet.stream().filter(x -> x.getMasterMan() != null).skip(page).limit(limit).collect(Collectors.toList()));
        }
        if (state.equals(3)) {
            return R.ok().data("page", pet.stream().skip(page).limit(limit).collect(Collectors.toList()));
        }

        return R.error().message("state 参数不对");
    }

    @GetMapping("/pet/for/user/{userId}/{state}")
    @ApiOperation("按用户搜索宠物【state=1寄养的，state=2领养的，state=3全部的】")
    public R searchPetForUser(@PathVariable("userId") Integer userId,
                    @PathVariable("state") Integer state) {
        // 查询宠物信息
        List<PetDto> pet = petService.all();

        if (state.equals(1)) {
            return R.ok().data("page", pet.stream().filter(x -> x.getFosterMan().getId().equals(userId)).collect(Collectors.toList()));
        }

        if (state.equals(2)) {
            return R.ok().data("page", pet.stream().filter(x->x.getMasterMan() != null).filter(x -> x.getMasterMan().getId().equals(userId)).collect(Collectors.toList()));
        }
        if (state.equals(3)) {
            List<PetDto> c1 = pet.stream().filter(x -> x.getFosterMan().getId().equals(userId)).collect(Collectors.toList());
            List<PetDto> c2 = pet.stream().filter(x->x.getMasterMan() != null).filter(x -> x.getMasterMan().getId().equals(userId)).collect(Collectors.toList());
            // c1 c2 合并
            c1.addAll(c2);
            return R.ok().data("page", c1);
        }

        return R.error().message("state 参数不对");
    }

    @GetMapping("/pet/for/room/{roomId}")
    @ApiOperation("按room搜索宠物")
    public R searchPetForRoom(@PathVariable("roomId") Integer roomId) {
        List<PetDto> collect = petService.all().stream()
                .filter(x -> x.getRoom().getId().equals(roomId))
                .collect(Collectors.toList());

        return R.ok().data("list", collect);
    }

    @GetMapping("/pet/for/kind/{kindId}")
    @ApiOperation("按kind搜索宠物")
    public R searchPetForKind(@PathVariable("kindId") Integer kindId) {
        List<PetDto> collect = petService.all().stream()
                .filter(x -> x.getKind().getId().equals(kindId))
                .collect(Collectors.toList());

        return R.ok().data("list", collect);
    }

}
